import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ParseIntPipe,
  UseInterceptors,
  UploadedFile,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiTags, ApiOperation, ApiResponse, ApiConsumes } from '@nestjs/swagger';
import { NavigationService } from './navigation.service';
import { UploadService } from './upload.service';
import {
  CreateNavigationDto,
  UpdateNavigationDto,
  NavigationResponseDto,
} from '../../dto/navigation.dto';
import { ResponseDto } from '../../common/dto/response.dto';

/**
 * 导航站点控制器
 */
@ApiTags('导航站点')
@Controller('navigation')
export class NavigationController {
  constructor(
    private readonly navigationService: NavigationService,
    private readonly uploadService: UploadService,
  ) {}

  /**
   * 获取导航站点列表
   */
  @Get()
  @ApiOperation({ summary: '获取导航站点列表' })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: [NavigationResponseDto],
  })
  async findAll(): Promise<NavigationResponseDto[]> {
    return await this.navigationService.findAll();
  }

  /**
   * 根据ID获取导航站点
   */
  @Get(':id')
  @ApiOperation({ summary: '根据ID获取导航站点' })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: NavigationResponseDto,
  })
  async findOne(@Param('id', ParseIntPipe) id: number) {
    return await this.navigationService.findOne(id);
  }

  /**
   * 创建导航站点
   */
  @Post()
  @ApiOperation({ summary: '创建导航站点' })
  @ApiResponse({
    status: 201,
    description: '创建成功',
  })
  async create(@Body() createNavigationDto: CreateNavigationDto) {
    return await this.navigationService.create(createNavigationDto);
  }

  /**
   * 更新导航站点
   */
  @Patch(':id')
  @ApiOperation({ summary: '更新导航站点' })
  @ApiResponse({
    status: 200,
    description: '更新成功',
  })
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateNavigationDto: UpdateNavigationDto,
  ) {
    return await this.navigationService.update(id, updateNavigationDto);
  }

  /**
   * 删除导航站点
   */
  @Delete(':id')
  @ApiOperation({ summary: '删除导航站点' })
  @ApiResponse({
    status: 200,
    description: '删除成功',
  })
  async remove(@Param('id', ParseIntPipe) id: number) {
    await this.navigationService.remove(id);
    return new ResponseDto(200, '删除成功');
  }

  /**
   * 切换导航站点启用状态
   */
  @Patch(':id/toggle')
  @ApiOperation({ summary: '切换导航站点启用状态' })
  @ApiResponse({
    status: 200,
    description: '切换成功',
  })
  async toggleActive(@Param('id', ParseIntPipe) id: number) {
    return await this.navigationService.toggleActive(id);
  }

  /**
   * 上传导航站点图标
   */
  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({ summary: '上传导航站点图标' })
  @ApiConsumes('multipart/form-data')
  @ApiResponse({
    status: 200,
    description: '上传成功',
    schema: {
      type: 'object',
      properties: {
        code: { type: 'number', example: 200 },
        message: { type: 'string', example: '上传成功' },
        data: {
          type: 'object',
          properties: {
            path: { type: 'string', example: '/uploads/navigation/1640995200000_abc123.png' }
          }
        },
        timestamp: { type: 'number', example: 1640995200000 }
      }
    }
  })
  async uploadFile(@UploadedFile() file: any) {
    const path = await this.uploadService.uploadFile(file);
    return { path };
  }
}